package com.whfc.ms.api.quality.controller;

import com.whfc.common.result.ListData;
import com.whfc.common.result.R;
import com.whfc.common.result.Result;
import com.whfc.common.result.ResultUtil;
import com.whfc.common.util.AssertUtil;
import com.whfc.common.util.SessionAttr;
import com.whfc.fuum.entity.SysUser;
import com.whfc.ms.api.quality.service.MsQualityIssueService;
import com.whfc.ms.config.AbstractBaseController;
import com.whfc.quality.dto.QualityIssueTypeDTO;
import com.whfc.quality.param.QualityIssueAddParam;
import com.whfc.quality.param.QualityIssueEditParam;
import com.whfc.quality.param.QualityIssueTypeAddParam;
import com.whfc.quality.param.QualityIssueTypeEditParam;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author : qzexing
 * @version : 1.0
 * @date : 2020-07-30 18:51
 */
@RequiresPermissions("quality/problem")
@RestController
@RequestMapping("/ms/api/quality/issue")
public class QualityIssueController extends AbstractBaseController {

    @Autowired
    private MsQualityIssueService msQualityIssueService;

    @GetMapping("/type/list")
    public Result typeList(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                           @RequestParam("deptId") Integer deptId,
                           @RequestParam(value = "keyword", required = false) String keyword) {
        logger.info("质量问题库类型列表,loginer:{},deptId:{},keyword:{}", user.getUsername(), deptId, keyword);
        List<QualityIssueTypeDTO> issueTypeList = msQualityIssueService.issueTypeList(deptId, keyword);
        return ResultUtil.success(new ListData<>(issueTypeList));
    }

    @RequiresPermissions("quality/problem/type")
    @PostMapping("/type/add")
    public Result typeAdd(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                          @RequestBody @Validated QualityIssueTypeAddParam request) {
        logger.info("新增质量问题库类型,loginer:{},request:{}", user.getUsername(), request);
        msQualityIssueService.addIssueType(request);
        return ResultUtil.success();
    }

    @RequiresPermissions("quality/problem/type")
    @PostMapping("/type/edit")
    public Result typeEdit(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                           @RequestBody @Validated QualityIssueTypeEditParam request) {
        logger.info("编辑质量问题库类型,loginer:{},request:{}", user.getUsername(), request);
        msQualityIssueService.editIssueType(request);
        return ResultUtil.success();
    }

    @RequiresPermissions("quality/problem/type")
    @PostMapping("/type/del/{issueTypeId}")
    public Result typeDel(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                          @PathVariable("issueTypeId") Long issueTypeId) {
        logger.info("删除质量问题库类型,loginer:{},issueTypeId:{}", user.getUsername(), issueTypeId);
        msQualityIssueService.delIssueType(issueTypeId);
        return ResultUtil.success();
    }

    @GetMapping("/list")
    public Result list(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                       @RequestParam("issueTypeId") Long issueTypeId,
                       @RequestParam(value = "pageSize", required = false) Integer pageSize,
                       @RequestParam(value = "pageNum", required = false) Integer pageNum) {
        logger.info("开始查询质量问题明细列表,loginer:{},issueTypeId:{}，pageSize:{},pageNum:{}", user.getUsername(), issueTypeId, pageSize, pageNum);
        if (pageSize != null && pageNum != null) {
            return ResultUtil.success(msQualityIssueService.issueList(issueTypeId, pageNum, pageSize));
        }
        return ResultUtil.success(new ListData<>(msQualityIssueService.issueList(issueTypeId)));
    }

    @GetMapping("/deptIdList")
    public Result issueList(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                            @RequestParam("deptId") Integer deptId) {
        logger.info("开始查询质量问题明细列表,loginer:{},deptId:{}", user.getUsername(), deptId);
        return ResultUtil.success(new ListData<>(msQualityIssueService.issueList(deptId)));
    }

    @RequiresPermissions("quality/problem/add")
    @PostMapping("/add")
    public Result add(@SessionAttribute(SessionAttr.MS_USER) SysUser user, @RequestBody @Validated QualityIssueAddParam addParam) {
        logger.info("新增质量问题明细,loginer:{},request:{}", user.getUsername(), addParam);
        msQualityIssueService.addIssue(addParam);
        return ResultUtil.success();
    }

    @RequiresPermissions("quality/problem/edit")
    @PostMapping("/edit")
    public Result edit(@SessionAttribute(SessionAttr.MS_USER) SysUser user, @RequestBody @Validated QualityIssueEditParam editParam) {
        logger.info("修改质量问题明细,loginer:{},request:{}", user.getUsername(), editParam);
        msQualityIssueService.editIssue(editParam);
        return ResultUtil.success();
    }

    @RequiresPermissions("quality/problem/delete")
    @PostMapping("/del/{issueId}")
    public Result del(@SessionAttribute(SessionAttr.MS_USER) SysUser user, @PathVariable("issueId") Long issueId) {
        logger.info("删除质量问题明细,loginer:{},issueId:{}", user.getUsername(), issueId);
        msQualityIssueService.delIssue(issueId);
        return ResultUtil.success();
    }

    @RequiresPermissions("quality/problem/enable")
    @GetMapping("/enable")
    public Result enable(@SessionAttribute(SessionAttr.MS_USER) SysUser user, @RequestParam("issueId") Long issueId,
                         @RequestParam("enable") Integer enable) {
        logger.info("删除质量问题明细,loginer:{},issueId:{},enable:{}", user.getUsername(), issueId, enable);
        msQualityIssueService.disableOrEnabledIssue(issueId, enable);
        return ResultUtil.success();
    }

    /**
     * 2.0质量问题库批量删除
     *
     * @param idList
     * @return
     */
    @RequiresPermissions("quality/problem/delete")
    @PostMapping("/delete/batch")
    public R deleteBatch(@RequestBody List<Integer> idList) {
        logger.info("质量问题库批量删除,userId:{},idList:{}", getLoginUser().getId(), idList);
        AssertUtil.isTrue(CollectionUtils.isEmpty(idList), "ID不能为空");
        msQualityIssueService.deleteBatch(idList);
        return R.success();
    }


}
